package com.tools.api;

import com.tools.commons.constants.SystemConstants;
import com.tools.commons.exceptions.BadRequestException;
import com.tools.commons.utils.BeanUtils;
import com.tools.model.Request.UserTaskListRequest;
import com.tools.model.entity.TasksEntity;
import com.tools.model.entity.UserEntity;
import com.tools.model.entity.UserHistoryRelEntity;
import com.tools.model.entity.UserMsgRelEntity;
import com.tools.model.params.*;
import com.tools.security.context.SecurityContextHolder;
import com.tools.security.utils.IsDoctor;
import com.tools.service.*;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangminggen
 */
@RestController
@RequestMapping(SystemConstants.Api.DOCTOR)
@RequiredArgsConstructor
public class DoctorController {
    private final InfoService infoService;
    private final TaskService taskService;
    private final UserService userService;
    private final UserTaskSerive userTaskSerive;
    private final UserMsgsService userMsgsService;
    private final UserHistoryService userHistoryService;


    @PostMapping("addTask")
    @ApiOperation("医生发布任务")
    public void addTask(@RequestBody @Valid TasksParams tasksEntity) {
        IsDoctor.check();
        taskService.saveTask(tasksEntity);
    }

    @PostMapping("addUserTaskByUserId")
    @ApiOperation("给病人添加任务")
    public void addUserTask(@RequestBody @Validated UserTaskParams userTasksEntity) {

        IsDoctor.check();
        UserEntity userEntity = userService.findByPhone(userTasksEntity.getPhone());
        if (userEntity == null) {
            throw new BadRequestException("无此病人，不可添加");
        }
        if (infoService.findByUserId(userEntity.getUserId()) == null) {
            throw new BadRequestException("病人不存在或未完善信息，不可发送消息");
        }
        if (taskService.findByTaskid(userTasksEntity.getTaskId()) == null) {
            throw new BadRequestException("无此任务");
        }
        if (userTaskSerive.findByTaskidAndUserid(userTasksEntity.getTaskId(), userEntity.getUserId()) != null) {
            throw new BadRequestException("任务已添加");
        }
        userTaskSerive.saveByTaskIdAndPhone(userTasksEntity.getTaskId(), userTasksEntity.getPhone());
    }

    /*   @PostMapping("addUserTaskByPhone")
       @ApiOperation("通过手机号给病人添加任务")
       public void addUserTaskByPhone(@RequestBody @Validated UserTaskPhoneParams userTaskPhoneParams) {

           IsDoctor.check();
           UserEntity userEntity =userService.findByPhone(userTaskPhoneParams.getPhone());
           if (userEntity ==null){
               throw new BadRequestException("无此病人，不可添加");
           }
           if(infoService.findByUserId(userEntity.getUserId())==null){
               throw new BadRequestException("病人不存在或未完善信息，不可发送消息");
           }
           if (taskService.findByTaskid(userTaskPhoneParams.getTaskId()) == null) {
               throw new BadRequestException("无此任务");
           }
           if (userTaskSerive.findByTaskidAndUserid(userTaskPhoneParams.getTaskId(), userEntity.getUserId()) != null) {
               throw new BadRequestException("任务已添加");
           }
           UserTaskParams userTasksEntity =new UserTaskParams();
           userTasksEntity.setTaskId(userTaskPhoneParams.getTaskId());
           userTasksEntity.set(userEntity.getUserId());
           userTaskSerive.save(userTasksEntity);
       }*/
    @ApiOperation("添加病人")
    @PostMapping("addUser")
    public void addUser(@RequestBody @Validated(UserParams.addUser.class) UserParams userParams) {
        IsDoctor.check();
        if (userService.findByPhone(userParams.getPhone()) != null) {
            throw new BadRequestException("病人已存在");
        }
        UserEntity userEntity1 = new UserEntity();
        userEntity1.setDoctor(false);
        userEntity1.setPhone(userParams.getPhone());
        userService.save(userEntity1);
    }

    @GetMapping("taskListByUserId")
    @ApiOperation("医生拥有的任务列表")
    public List<TasksEntity> GetTaskListById() {
        IsDoctor.check();
        return taskService.findTaskListByUserId(SecurityContextHolder.getContext().getUserInfo().getUserId());
    }

    // @GetMapping("taskUserList")
    // @ApiOperation("task的user列表")
    // public List<UserTasksEntity> GetUserTaskListByTaskId(String taskId) {
    //     IsDoctor.check();
    //     if (taskService.findByTaskid(taskId) == null) {
    //         throw new BadRequestException("无此任务");
    //     }
    //     return userTaskSerive.findTaskListByTaskId(taskId);
    // }

    //查看详情
    @GetMapping("taskDetail")
    @ApiOperation("taskDetail列表")
    public TasksEntity taskDetail(String taskId) {
        if (taskService.findByTaskid(taskId) == null) {
            throw new BadRequestException("无此任务");
        }
        return taskService.findByTaskid(taskId);
    }

    //task的user表
    @GetMapping("taskUserList")
    @ApiOperation("task的users列表")
    public List<UserTaskListRequest> GetUserByTaskId(String taskId) {
        IsDoctor.check();
        if (taskService.findByTaskid(taskId) == null) {
            throw new BadRequestException("无此任务");
        }
        return userTaskSerive.findUsersByTaskid(taskId);
    }

    @ApiOperation("给病人发送消息")
    @PostMapping("sendMsg")
    public void addMsg(@RequestBody @Validated UserMsgParams userMsgParams) {
        IsDoctor.check();
        ExistUserDetail(userMsgParams.getTo());
        UserMsgRelEntity userMsgRelEntity = new UserMsgRelEntity();
        BeanUtils.updateProperties(userMsgParams, userMsgRelEntity);
        userMsgRelEntity.setFrom(SecurityContextHolder.getContext().getUserInfo().getUserId());
        userMsgRelEntity.setStatus(0);
        userMsgsService.save(userMsgRelEntity);

    }

    @ApiOperation("添加病史")
    @PostMapping("addHistory")
    public void addHistory(@RequestBody @Validated UserHistoryParams userHistoryParams) {
        IsDoctor.check();
        ExistUserDetail(userHistoryParams.getUserId());
        userHistoryService.save(userHistoryParams);

    }

    @ApiOperation("查找病史")
    @PostMapping("History")

    public List<UserHistoryRelEntity> findHistoryByUserId(String userId) {
        IsDoctor.check();
        return userHistoryService.findByUserId(userId);
    }


    public void ExistUserDetail(String id) {
        if (infoService.findByUserId(id) == null) {
            throw new BadRequestException("病人不存在或未完善信息，不可发送消息");
        }
    }
}
